package com.lg.happyfarmer.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.widget.Button;
import android.widget.FrameLayout;

import com.hemaapp.hm_FrameWork.view.HemaWebView;
import com.lg.happyfarmer.R;
import com.lg.happyfarmer.base.BaseUtil;

/**
 * Auther:"zmk"
 * Created:"2017/6/7" "9:10"
 * Desc:"升级提示框，可编辑升级说明"
 * CopyRight:""
 * Version ""
 */

public class UpdateConfirmDialog extends Dialog implements View.OnClickListener {

    private Context mContext;//上下文

    private HemaWebView webView;//图文详情
    private Button positiveButton;//确定
    private Button negativeButton;//取消

    private UpdateDialogConfirmClick comfirmClick;//确定回调

    public UpdateConfirmDialog(@NonNull Context mContext, String path) {
        super(mContext);
        this.mContext = mContext;
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_update_confirm);
        findViewById();
        setListener();
        //升级说明
        webView.loadUrl(path);
        //webview高度设置
        int height = BaseUtil.getScreenHeight(mContext) / 2;
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) webView.getLayoutParams();
        params.height = height;
        webView.setLayoutParams(params);
    }

    //获取控件
    public void findViewById() {
        positiveButton = (Button) findViewById(R.id.positiveButton);
        negativeButton = (Button) findViewById(R.id.negativeButton);
        webView = (HemaWebView) findViewById(R.id.webview);
        initWebView(webView);
    }

    /**
     * 初始化webview
     */
    private void initWebView(HemaWebView webview) {
        // 支持javascript
        webview.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webview.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webview.getSettings().setBuiltInZoomControls(false);
        // 扩大比例的缩放
        webview.getSettings().setUseWideViewPort(true);
        // 自适应屏幕
        webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webview.getSettings().setLoadWithOverviewMode(true);
        // 取消显示滚动条
        webview.setVerticalScrollBarEnabled(false);
        webview.setHorizontalScrollBarEnabled(false);
        webview.setFocusable(false);
    }

    //设置监听
    public void setListener() {
        positiveButton.setOnClickListener(this);
        negativeButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.negativeButton://取消
                dismiss();
                if(comfirmClick != null){
                    comfirmClick.onCancel();
                }
                break;
            case R.id.positiveButton://确定
                dismiss();
                if (comfirmClick != null) {
                    comfirmClick.onConfirm();
                }
                break;
            default:
                break;
        }
    }

    public void setComfirmClick(UpdateDialogConfirmClick comfirmClick) {
        this.comfirmClick = comfirmClick;
    }

    //确认回调
    public interface UpdateDialogConfirmClick {
        public void onConfirm();
        public void onCancel();
    }
}
